export class IndexService {
  constructor() {}

  verifyStep(activeIndex, formData) {
    let result = {};
    let rules = [];
    switch (activeIndex) {
      case 1:
        rules = [
          { rule: formData.supplier !== '', message: '请选择供应商' },
          { rule: formData.contract !== '', message: '请选择合同' }
        ];
        result = checkRule(rules);
        break;
      // case 2:
      //   rules = [{ rule: formData.products.length > 0, message: '请添加产品' }];
      //   result = checkRule(rules);
      //   break;
      case 3:
        rules = [{ rule: formData.carryway !== '', message: '请选择承运方式' }];
        result = checkRule(rules);
        if (result.pass === false) {
          return result;
        }

        if (formData.carryway !== '1') {
          rules = [{ rule: formData.address !== '', message: '请选择收货地址' }];
          result = checkRule(rules);
        }
        break;
      default:
        result.pass = true;
        break;
    }
    return result;
  }

  mappingSubmitData(submitData, formData) {
    let order_data = JSON.parse(JSON.stringify(submitData));
    order_data.page = 3;
    order_data.remark = formData.remark;
    order_data.transportType = formData.carryway;
    if (order_data.transportType === '1') {
      //qiyu 2020-4-3 自提的时候，在提交的时候清空收货地址 by 王康
      order_data.morderReceiverForms = {};
    } else {
      order_data.morderReceiverForms.addressPhone = formData.address_full.userPhone;
      order_data.morderReceiverForms.addressee = formData.address_full.userName;
      order_data.morderReceiverForms.area = formData.address_full.area;
      order_data.morderReceiverForms.city = formData.address_full.city;
      order_data.morderReceiverForms.province = formData.address_full.province;
      order_data.morderReceiverForms.detail = formData.address_full.detailedAddress;
    }
    return order_data;
  }

  mappingAppend2Extend(appendData) {
    let extend_data = [];
    appendData.map(item => {
      let extend_item = { type: 'input', id: item.orderAppendId, label: item.attributeName };
      extend_data.push(extend_item);
    });
    return extend_data;
  }

  mappingOrderDetail(orderDetail) {
    let form_data = {
      supplier: orderDetail?.sorderContractForm?.supplierCode,
      contract: orderDetail?.sorderContractForm?.contractCode,
      address: orderDetail?.morderReceiverForms?.id,
      extend: {},
      products: orderDetail?.purchaseProduct,
      id: orderDetail.orderId,
      detail_id: orderDetail.orderDetailId,
      remark: orderDetail.remark,
      carryway: String(orderDetail.transportType),
      operation: orderDetail.operationType
    };
    return form_data;
  }
  mappingOrderDetailReadonly(orderDetail) {
    let formdata = {
      supplier: orderDetail.supplierName,
      contract: orderDetail.contractName,
      vendor: orderDetail.originalFactoryName,
      product_line: orderDetail.productLineName
    };
    return formdata;
  }

  diffContract(formData, oldFormData) {
    if (formData.contract !== oldFormData.contract) {
      return true;
    } else {
      return false;
    }
  }
  resetOrder(formData) {
    let form_date = JSON.parse(JSON.stringify(formData));

    form_date.carryway = '';
    //...
    return form_date;
  }
}

/**
 * 检查规则，遇到rule=false的情况，立即扔出结果
 * @param {rule:bool, message:string} verifyRules
 */
function checkRule(verifyRules) {
  let result = { pass: true, message: '' };
  for (let i = 0; i < verifyRules.length; i++) {
    const element = verifyRules[i];
    if (element.rule === true) {
      result.pass = true;
    } else {
      result.message = element.message;
      result.pass = false;
      return result;
    }
  }
  return result;
}
